package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Myht;
import com.hopu.pojo.Zc;
import com.hopu.service.HtService;
import com.hopu.util.FileUtil;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RequestMapping("/ht")
@RestController
public class HtController {
    @Resource
    private HtService htServiceImpl;
    @PostMapping("/findAllHt")
    private ResultUtils findAllHt(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Myht> allHt = htServiceImpl.findAllHt();

            PageInfo pageInfo = new PageInfo(allHt);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("addHt")
    public ResultUtils addHt(
            HttpServletRequest request, HttpServletResponse response,
            MultipartHttpServletRequest multipartRequest,
            @RequestParam(value = "htname")String htname
    ){
        try {
            //Emp empLog = (Emp) request.getSession().getAttribute("empLog");
            Iterator<String> it = multipartRequest.getFileNames();
            String filePath = "";
            while (it.hasNext()){
                filePath = FileUtil.upload(multipartRequest.getFile((String) it.next()));
            }
            int num = htServiceImpl.addHt(htname);

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/findByHtid")
    public ResultUtils findHtByHtid(int htid){

        Myht myht = htServiceImpl.findHtByHtid(htid);

        return new ResultUtils(200,"查询页面成功",myht);

    }
    @PostMapping("uppHt")
    public ResultUtils uppHt(Myht myht,MultipartHttpServletRequest multipartRequest,
                             HttpSession session){



        int biao1 = htServiceImpl.uppHt(myht);
        if (biao1<0){
            return new ResultUtils(400,"ERROR",null);
        }
        return new ResultUtils(200,"SUCCESS",1);
    }
}
